package com.xmut.bs.handaccount.activity;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.xmut.bs.handaccount.GlideApp;
import com.xmut.bs.handaccount.R;
import com.xmut.bs.handaccount.base.CommonListActivity;
import com.xmut.bs.handaccount.bmob.Follow;
import com.xmut.bs.handaccount.util.DisplayUtils;
import com.xmut.bs.handaccount.util.DividerItemDecoration;
import com.xmut.bs.handaccount.util.GlideCircleTransform;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.List;

import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;

/**
 * Description
 * Created by Dollkey on 2018/4/11.
 */

public class MyFollowActivity extends CommonListActivity<Follow> {
    @Override
    protected String getSubTitle() {
        return "我的关注";
    }

    @Override
    protected RecyclerView.ItemDecoration getItemDecoration() {
        return new DividerItemDecoration(mContext,
                DisplayUtils.dp2px(mContext, 1),
                getResources().getColor(R.color.grey200)).setHasLeftOrRight(false).setHasTopAndBottom(false);
    }

    @NonNull
    @Override
    protected RecyclerView.LayoutManager getRvLayoutManager() {
        return new LinearLayoutManager(mContext);
    }

    @Override
    protected RecyclerView.Adapter getAdapter(List<Follow> comList) {
        return new CommonAdapter<Follow>(mContext, R.layout.item_user, comList) {
            @Override
            protected void convert(ViewHolder holder, final Follow follow, int position) {
                holder.getConvertView().setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
//                        startActivity(new Intent().putExtra(PARAM_ARTICLE_ID, article), ArticleDetailActivity.class);
                    }
                });
                holder.setText(R.id.username, follow.getFollowedUser().getNickName());
                ImageView avatarImg = holder.getView(R.id.avatar_img);
                GlideApp.with(avatarImg).load(follow.getFollowedUser() == null ? "" : follow.getFollowedUser().getAvatar())
                        .placeholder(R.mipmap.ic_head_img).error(R.mipmap.ic_head_img)
                        .transform(new GlideCircleTransform(mContext)).override(60, 60).into(avatarImg);
            }
        };
    }

    @Override
    public void getData(int pageNo, int pageSize) {
        if (pageNo != 0) return;
        mBmobQuery.addWhereEqualTo("followUser", mUser);
        mBmobQuery.include("followedUser");
        mBmobQuery.findObjects(new FindListener<Follow>() {
            @Override
            public void done(List<Follow> list, BmobException e) {
                if (e == null) {
                    showList(list);
                } else {
                    showError("请求失败：" + e.getMessage());
                }
            }
        });
    }

    @Override
    protected void initParams(Intent intent) {

    }
}
